import 'package:dio/dio.dart';
import 'package:html/parser.dart' show parse;
import 'package:html/dom.dart';

void main() async {
  print("start");
  await Spider.getLatestBook();
  print("stop");
}

String baseUrl = "https://www.biquge.com.cn/";

class Spider {
  static getLatestBook() async {
    // 获取书籍的详情信息
    // todo 先查询数据库中是否存了
    Response resp = await Dio().get(baseUrl);
    String data = resp.data;
    // 解析页面元素
    Document documnet = parse(data);
    // 解析book相关信息
    List<Element> latestUpdatebooks = documnet
        .getElementById("newscontent")
        .querySelector(".l")
        .getElementsByTagName('li');
    List latestUpdatebookList = [];
    for (var item in latestUpdatebooks) {
      latestUpdatebookList.add({
        "type": item.querySelector(".s1").innerHtml,
        "title":
            item.querySelector(".s2").getElementsByTagName("a")[0].innerHtml,
        "url": baseUrl +
            item
                .querySelector(".s2")
                .getElementsByTagName("a")[0]
                .attributes["href"],
        "latestChapterName":
            item.querySelector(".s3").getElementsByTagName("a")[0].innerHtml,
        "latestChapterUrl": baseUrl +
            item
                .querySelector(".s3")
                .getElementsByTagName("a")[0]
                .attributes["href"],
        "author": item.querySelector(".s4").innerHtml,
        "updateTime": item.querySelector(".s5").innerHtml
      });
    }
    // print(latestUpdatebookList);
    List<Element> latestRecordBooks = documnet
        .getElementById("newscontent")
        .querySelector(".r")
        .getElementsByTagName('li');
    List latestRecordBookList = [];
    for (var item in latestRecordBooks) {
      latestRecordBookList.add({
        "type": item.querySelector(".s1").innerHtml,
        "title":
            item.querySelector(".s2").getElementsByTagName("a")[0].innerHtml,
        "url": baseUrl +
            item
                .querySelector(".s2")
                .getElementsByTagName("a")[0]
                .attributes["href"],
        "updateTime": item.querySelector(".s5").innerHtml
      });
    }
    // print(latestRecordBookList);
    Map result = {
      "latestRecordBookList": latestRecordBookList,
      "latestUpdatebookList": latestUpdatebookList
    };
    return result;
  }


}

